package service

import (
	"context"
	"strconv"
	__ "user_service/basic/proto"
	"user_service/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {

	var u model.User
	u.GetUserBy(in.Mobile)

	if u.Id != 0 {
		return &__.RegisterResp{
			Code: 10000,
			Msg:  "用户已存在",
		}, nil
	}

	u = model.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}

	u.Create()

	return &__.RegisterResp{
		Code: 200,
		Msg:  "注册成功",
	}, nil
}

func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var u model.User
	u.GetUserBy(in.Mobile)

	if u.Id == 0 {
		return &__.LoginResp{
			Code: 10000,
			Msg:  "用户不存在",
		}, nil
	}

	if u.Password == in.Password {
		return &__.LoginResp{
			Code: 10000,
			Msg:  "密码错误",
		}, nil
	}

	return &__.LoginResp{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}

func (s *Server) AddTest(_ context.Context, in *__.AddTestReq) (*__.AddTestResp, error) {
	var t model.TestQuestions
	t.GetTestBy(strconv.FormatInt(in.Id, 10))

	t = model.TestQuestions{
		QuestionsType: uint(in.QuestionsType),
		Difficulty:    uint(in.Difficulty),
		Cate1:         in.Cate1,
		Cate2:         in.Cate2,
		Author:        in.Author,
		TestQuestion:  in.TestQuestions,
		AuthorId:      uint(in.AuthorId),
		Founder:       in.Founder,
		FounderId:     uint(in.FounderId),
		CreateTime:    in.CreateTime,
		Reviewer:      in.Reviewer,
		ReviewerId:    uint(in.ReviewerId),
		ValueA:        in.ValueA,
		ValueB:        in.ValueB,
		ValueC:        in.ValueC,
		ValueD:        in.ValueD,
		OptionAnswer:  uint(in.OptionAnswer),
		Analysis:      in.Analysis,
		Keywords:      in.Keywords,
		FounderName:   in.FounderName,
		ViewingRange:  uint(in.ViewingRange),
		EnableStatus:  in.EnableStatus,
	}

	t.Create()

	return &__.AddTestResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}

func (s *Server) UpdateTest(_ context.Context, in *__.UpdateTestReq) (*__.UpdateTestResp, error) {
	var t model.TestQuestions

	t = model.TestQuestions{
		Id:            uint(in.Id),
		QuestionsType: uint(in.QuestionsType),
		Difficulty:    uint(in.Difficulty),
		Author:        in.Author,
		TestQuestion:  in.TestQuestions,
		ValueA:        in.ValueA,
		ValueB:        in.ValueB,
		ValueC:        in.ValueC,
		ValueD:        in.ValueD,
		OptionAnswer:  uint(in.OptionAnswer),
		Analysis:      in.Analysis,
		Keywords:      in.Keywords,
	}

	t.Update()

	return &__.UpdateTestResp{
		Code: 200,
		Msg:  "修改成功",
	}, nil
}

func (s *Server) InfoTest(_ context.Context, in *__.InfoTestReq) (*__.InfoTestResp, error) {
	var t model.TestQuestions
	err := t.Info(uint(in.Id))
	if err != nil {
		return nil, err
	}

	return &__.InfoTestResp{
		QuestionsType: int64(t.QuestionsType),
		Difficulty:    int64(t.Difficulty),
		Cate1:         t.Cate1,
		Cate2:         t.Cate2,
		Author:        t.Author,
		TestQuestion:  t.TestQuestion,
		AuthorId:      int64(t.AuthorId),
		Founder:       t.Founder,
		FounderId:     int64(t.FounderId),
		CreateTime:    t.CreateTime,
		Reviewer:      t.Reviewer,
		ReviewerId:    int64(t.ReviewerId),
		ValueA:        t.ValueA,
		ValueB:        t.ValueB,
		ValueC:        t.ValueC,
		ValueD:        t.ValueD,
		OptionAnswer:  int64(t.OptionAnswer),
		Analysis:      t.Analysis,
		Keywords:      t.Keywords,
		FounderName:   t.FounderName,
		ViewingRange:  int64(t.ViewingRange),
		EnableStatus:  t.EnableStatus,
		Id:            int64(t.Id),
	}, nil
}

func (s *Server) DelTest(_ context.Context, in *__.DelTestReq) (*__.DelTestResp, error) {
	var t model.TestQuestions

	t.Del(uint(in.Id))

	return &__.DelTestResp{
		Code: 200,
		Msg:  "删除成功",
	}, nil
}

func (s *Server) AddPaper(_ context.Context, in *__.AddPaperReq) (*__.AddPaperResp, error) {
	var t model.TestPaper

	t = model.TestPaper{
		PaperType:       uint(in.PaperType),
		PaperName:       in.PaperName,
		PaperTime:       in.PaperTime,
		ArrangementType: uint(in.ArrangementType),
		OptionType:      uint(in.OptionType),
		Score:           uint(in.Score),
		PassingScore:    uint(in.PassingScore),
		Reviewer:        in.Reviewer,
		Known:           in.Known,
		SingleScore:     uint(in.SingleScore),
	}

	t.Create()

	return &__.AddPaperResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}

func (s *Server) InfoPaper(_ context.Context, in *__.InfoPaperReq) (*__.InfoPaperResp, error) {
	var t model.TestQuestions
	t.Info(uint(in.Id))

	return &__.InfoPaperResp{
		TestQuestion:  t.TestQuestion,
		QuestionsType: int64(t.QuestionsType),
		Keywords:      t.Keywords,
		Cate1:         t.Cate1,
		Difficulty:    int64(t.Difficulty),
	}, nil
}

func (s *Server) DelPaper(_ context.Context, in *__.DelPaperReq) (*__.DelPaperResp, error) {
	var t model.TestPaper
	t.Del(uint(in.Id))

	return &__.DelPaperResp{
		Code: 200,
		Msg:  "删除成功",
	}, nil

}

func (s *Server) ListPaper(_ context.Context, in *__.ListPaperReq) (*__.ListPaperResp, error) {
	var t model.TestPaper
	list, err := t.List()
	if err != nil {
		return nil, err
	}

	var paper []*__.PaperList

	for _, m := range list {
		lis := &__.PaperList{
			Id:              int64(m.Id),
			PaperType:       int64(m.PaperType),
			PaperName:       m.PaperName,
			PaperTime:       m.PaperTime,
			ArrangementType: int64(m.ArrangementType),
			OptionType:      int64(m.OptionType),
			Score:           int64(m.Score),
		}

		paper = append(paper, lis)
	}

	return &__.ListPaperResp{
		List: paper,
	}, nil
}

func (s *Server) List(_ context.Context, in *__.ListReq) (*__.ListResp, error) {
	var t model.TestQuestions
	list, err := t.List()
	if err != nil {
		return nil, err
	}

	var test []*__.List

	for _, m := range list {
		lis := &__.List{
			Id:           int64(m.Id),
			TestQuestion: m.TestQuestion,
			ValueA:       m.ValueA,
			ValueB:       m.ValueB,
			ValueC:       m.ValueC,
			ValueD:       m.ValueD,
			OptionAnswer: int64(m.OptionAnswer),
			Analysis:     m.Analysis,
		}
		test = append(test, lis)
	}
	return &__.ListResp{
		List: test,
	}, nil
}

func (s *Server) Info(_ context.Context, in *__.InfoReq) (*__.InfoResp, error) {
	var t model.Test

	t.Info(in.ProjectName)

	return &__.InfoResp{
		Id:          int64(t.Id),
		ProjectName: t.ProjectName,
		TestName:    t.TestName,
		Cate:        t.Cate,
		Name:        t.Name,
		CTime:       t.CTime,
	}, nil
}

func (s *Server) Add(_ context.Context, in *__.AddReq) (*__.AddResp, error) {
	var t model.Test

	t = model.Test{
		UserId:      uint(in.UserId),
		PaperId:     uint(in.PaperId),
		ProjectName: in.ProjectName,
		TestName:    in.TestName,
		Cate:        in.Cate,
		Name:        in.Name,
		CTime:       in.CTime,
	}

	t.Create()

	return &__.AddResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil

}

func (s *Server) InfoTes(_ context.Context, in *__.InfoTesReq) (*__.InfoTesResp, error) {
	var t model.Test
	t.InfoTes(strconv.FormatInt(in.Id, 10))

	return &__.InfoTesResp{
		Id:          int64(t.Id),
		UserId:      int64(t.UserId),
		PaperId:     int64(t.PaperId),
		ProjectName: t.ProjectName,
		TestName:    t.TestName,
		Cate:        t.Cate,
		Name:        t.Name,
		CTime:       t.CTime,
	}, nil
}

func (s *Server) InfoTe(_ context.Context, in *__.InfoTeReq) (*__.InfoTeResp, error) {
	var t model.Test
	t.InfoTe(strconv.FormatInt(in.UserId, 10))

	return &__.InfoTeResp{
		Id:          int64(t.Id),
		UserId:      int64(t.UserId),
		PaperId:     int64(t.PaperId),
		ProjectName: t.ProjectName,
		TestName:    t.TestName,
		Cate:        t.Cate,
		Name:        t.Name,
		CTime:       t.CTime,
	}, nil
}

func (s *Server) ITest(_ context.Context, in *__.ITestReq) (*__.ITestResp, error) {

	var t model.Test
	t.InfoTes(strconv.FormatInt(in.TestStatus, 10))

	return &__.ITestResp{
		UserId:      int64(t.UserId),
		PaperId:     int64(t.PaperId),
		ProjectName: t.ProjectName,
		TestName:    t.TestName,
		Cate:        t.Cate,
		Name:        t.Name,
		CTime:       t.CTime,
	}, nil

}
